<?php
/**
 * Created by PhpStorm.
 * User: zyj
 * Date: 2019/2/15
 * Time: 15:21
 */

namespace app\admin\model;


use think\Db;

class GiveRecord extends DataBase
{
    protected $name = 'give_record';

    public $prizeType = [1=>'金币','钻石',3=>'座驾','礼物',5=>'喇叭','生肖卡'];
    //public $prizeType = [4=>'座驾',6=>'喇叭'];


    public function getDataList()
    {
        $u_id = (int)(input('user_id'));
        $newData['user_id'] =  User::where('u_id','=',$u_id)->value('id');
        $newData['record_number'] = (int)input('prize_number');

        $remark =trim(input('remark'));
//        $account =input('account');
        $newData['remark'] = $remark;
        $newData['create_time'] = time();
        $newData['record_type'] = 2;
        $newData['operator_name'] = session('admin_name');
        $newData['operator_id'] = session('admin_id');


        // 启动事务
        $this->startTrans();
        try {

            $this->save($newData);
            $a1 = (int)User::where('u_id','=',$u_id)->value('diamonds');

            $res = $a1 - $newData['record_number'];
            if($res < 0) {
                return ['code'=>-100,'msg'=>'余额不足'];
            }else{
                User::where('u_id','=',$u_id)->update(['diamonds'=>$res]);
                $this->commit();
                return ['code'=>100,'msg'=>'提交成功'];
            }




        }catch(\Exception $e){
            $this->rollback();
            return ['code'=>-100,'msg'=>'提交失败'.$e->getMessage()];
        }
    }

    public function getIndexData() // GiveRecordIndexData
    {


        $page_config = [
            'page'=>input('offset',1),
            'list_rows'=>input('limit',10),
        ];

        $key = input('get.key');
        $value = input('get.value');
        $where = [];

        if($key == 'u_id' && $value){
            $user_id = User::where('u_id',$value)->value('id');
            $where[] = ['user_id','eq',$user_id];
        }
        if($key == 'account' && $value)
            $where[] = ['remark','like','%'.$value.'%'];

        if($key == 'admin' && $value)
            $where[] = ['remark','like','%'.$value.'%'];

        // $order = input('sort','create_time').' '.input('order','DESC');

        $data = $this->where($where)->order('id desc')->paginate($page_config);

        // dump($data);

        foreach($data as $key=>$value){

            $userInfo = User::where('id','=',$value['user_id'])->field('u_id,nickname')->find();

            $data[$key]['u_id'] = $userInfo['u_id'];
            $data[$key]['nickname'] = $userInfo['nickname'];
            $type = $value['record_type'];
            $data[$key]['prize_type'] = $this->prizeType[$type];
            switch($type){
                case 1: $data[$key]['gift_name'] = '金币'; break;
                case 2: $data[$key]['gift_name'] = '钻石'; break;
                case 3:
                    $data[$key]['gift_name'] = Vehicle::where('vehicle_id','=',$value['prize_id'])->value('vehicle_name');
                    break;
                case 4:
                    $data[$key]['gift_name'] = Gift::where('id','=',$value['prize_id'])->value('gift_name');
                    break;
                case 5:
                    $data[$key]['gift_name'] = ShopHorn::where('horn_id','=',$value['prize_id'])->value('horn_name');
                    break;
                case 6:
                    $data[$key]['gift_name'] = Card::where('id','=',$value['prize_id'])->value('name');
                    break;

                default: $data[$key]['gift_name'] = '未知';

            }
            $data[$key]['prize_number'] = $value['record_number'];
            $data[$key]['remark'] = $value['remark'];

            $data[$key]['create_time'] =date('Y-m-d H:i:s',$value['create_time']);

        }
        //  dump($data);
        return $data;
    }



}